﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ClientAppViaWebService.DBAdministration
{
    public partial class WebServicesSettingForm : Form
    {
        public WebServicesSettingForm()
        {
            InitializeComponent();

        }

        private void WebServicesSettingForm_Load(object sender, EventArgs e)
        {
            try
            {
                dtgWebServicesSettings.DataSource = LoadWebServicesSettings();
                dtgWebServicesSettings.Columns[0].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
                dtgWebServicesSettings.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            }
            catch(Exception ex)
            {
                MessageBox.Show("Failed  to load configuration. " + ex.Message);
            }

        }

        private void SaveWebServicesSettings(List<KeyValue> newSettingsDict)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ClientSettingsSection clientSection =
            config.SectionGroups["applicationSettings"]
            .Sections["ClientAppViaWebService.Properties.Settings"]
            as ClientSettingsSection;

            SettingElementCollection newSettings = new SettingElementCollection();
            SettingValueElement value;

            foreach (SettingElement set in clientSection.Settings)
            {
                value = set.Value as SettingValueElement;
                set.Value.ValueXml.InnerText = newSettingsDict.Where(i => set.Name.EndsWith( i.Key ) ).FirstOrDefault().Value;

                newSettings.Add(set);
           }

            clientSection.Settings.Clear();

            foreach (SettingElement newSet in newSettings)
            {
                clientSection.Settings.Add(newSet);
                ConfigurationManager.RefreshSection(newSet.Name);  
            }

            config.Save(ConfigurationSaveMode.Modified);
            //// Force a reload of a changed section.
            ConfigurationManager.RefreshSection("applicationSettings");  
            
        }

        private List<KeyValue> LoadWebServicesSettings()
        {
            List<KeyValue> settingsDictionary = new List<KeyValue>();

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ClientSettingsSection clientSection =
            config.SectionGroups["applicationSettings"]
            .Sections["ClientAppViaWebService.Properties.Settings"]
            as ClientSettingsSection;
         
            SettingValueElement value;

            foreach (SettingElement set in clientSection.Settings)
            {
                value = set.Value as SettingValueElement;
                settingsDictionary.Add(new KeyValue( 
                    set.Name.Remove(0,"Client_App___via_web_service___".Length),
                    value.ValueXml.InnerText));
            }

            return settingsDictionary;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            SaveWebServicesSettings(dtgWebServicesSettings.DataSource as List<KeyValue>);
        }

        public class KeyValue
        {
            public string Key{get;set;}
            public string Value { get; set; }

            public KeyValue(string k, string v)
            { Key = k; Value = v; }
        }
    }
}

